<template>
  <div class="courses-container">
    <div class="page-header">
      <h2>我的课程</h2>
      <el-button type="primary" icon="el-icon-plus" @click="$router.push('/course/create')">创建课程</el-button>
    </div>
    
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>课程列表</span>
        <el-input
          placeholder="搜索课程"
          v-model="searchQuery"
          style="width: 200px; float: right"
          prefix-icon="el-icon-search"
          clearable
          @clear="fetchCourses"
          @keyup.enter.native="handleSearch"
        />
      </div>
      
      <el-table
        :data="courseList"
        style="width: 100%"
        v-loading="loading"
      >
        <el-table-column prop="title" label="课程名称" width="250"></el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'info'">
              {{ scope.row.status === 1 ? '已发布' : '草稿' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="price" label="价格" width="100">
          <template slot-scope="scope">
            {{ scope.row.price === 0 ? '免费' : '¥' + scope.row.price }}
          </template>
        </el-table-column>
        <el-table-column prop="enrollCount" label="学生数量" width="100"></el-table-column>
        <el-table-column prop="lessonCount" label="课时数" width="100"></el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="$router.push(`/course/edit/${scope.row.id}`)">编辑</el-button>
            <el-button size="mini" type="success" @click="handlePublish(scope.row)" v-if="scope.row.status !== 1">发布</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { mapGetters } from 'vuex';
import { getTeacherCourses, deleteCourse, updateCourse } from '@/api/course';

export default {
  name: 'CourseList',
  data() {
    return {
      courseList: [],
      loading: false,
      currentPage: 1,
      pageSize: 10,
      total: 0,
      searchQuery: ''
    };
  },
  computed: {
    ...mapGetters(['teacherId'])
  },
  created() {
    this.fetchCourses();
  },
  methods: {
    fetchCourses() {
      if (!this.teacherId) return;
      
      this.loading = true;
      getTeacherCourses(this.teacherId, this.currentPage, this.pageSize)
        .then(response => {
          const { data } = response;
          if (data && data.records) {
            this.courseList = data.records;
            this.total = data.total || 0;
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchCourses();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchCourses();
    },
    handleSearch() {
      // 实现搜索功能
      this.currentPage = 1;
      this.fetchCourses();
    },
    handlePublish(course) {
      const updatedCourse = { ...course, status: 1 };
      updateCourse(updatedCourse).then(() => {
        this.$message({
          type: 'success',
          message: '课程发布成功!'
        });
        this.fetchCourses();
      });
    },
    handleDelete(id) {
      this.$confirm('确认删除该课程吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteCourse(id).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.fetchCourses();
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.courses-container {
  .page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    
    h2 {
      margin: 0;
      font-size: 22px;
      font-weight: bold;
    }
  }
  
  .pagination-container {
    margin-top: 20px;
    text-align: right;
  }
}
</style> 